package com.yuanian.ecsplus.advice;

import com.yuanian.ecsplus.exception.EcsPlusException;
import com.yuanian.infrastructure.util.model.ResultValue;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ControllerExceptionAdvice {

    @ExceptionHandler({EcsPlusException.class})
    public ResultValue controllerResultException(EcsPlusException e) {
        e.printStackTrace();
        //回滚事务
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return new ResultValue(e, false, e.getCause().getMessage());
    }

    @ExceptionHandler({Exception.class})
    public ResultValue exceptionHandle(Exception e) {
        e.printStackTrace();
        //回滚事务
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return new ResultValue(e.getMessage(), false, "请求失败，请联系管理员！");
    }

}
